<template>
  <div class="tags-container">
    <el-card>
      <!-- 面包屑 -->
        <div slot="header" v-if="subject.id && subject.name">
          <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item>学科管理</el-breadcrumb-item>
            <el-breadcrumb-item>{{subject.name}}</el-breadcrumb-item>
            <el-breadcrumb-item>目录管理</el-breadcrumb-item>
          </el-breadcrumb>
        </div>
      <!-- 栅格系统 -->
      <el-row>
        <!-- 栅格系统-左侧 -->
        <el-col :span="18">
          <el-form label-width="80px" size="small" :inline="true" >
            <el-form-item label="标签名称">
              <el-input v-model="requestParams.tagName"></el-input>
            </el-form-item>
          <el-form-item label="状态">
            <el-select v-model="requestParams.state">
              <el-option :value="1" label="启用"></el-option>
              <el-option :value="0" label="禁用"></el-option>
            </el-select>
          </el-form-item>
            <el-form-item>
              <el-button @click="clear">清除</el-button>
              <el-button @click="screen()" type="primary">搜索</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <!-- 栅格系统-右侧 -->
        <el-col :span="6" style="text-align:right">
          <el-button v-if="subject.id && subject.name" type="text" icon="el-icon-back" @click="$router.back()">返回学科</el-button>
          <el-button icon="el-icon-edit" type="success" size="small" @click="showDialogue()">新增目录</el-button>
        </el-col>
      </el-row>
      <!-- 提示框 -->
      <el-alert
        :title="`数据一共 ${total} 条`"
        type="info"
        show-icon
        >
      </el-alert>
      <!-- 表格显示 -->
      <el-table
        :data="tags"
        style="width: 100%">
        <!-- 序号 -->
        <el-table-column type="index" label="序号" width="80px"></el-table-column>
        <!-- 所属学科 -->
        <el-table-column label="所属学科" prop="subjectName" width="180"></el-table-column>
        <!-- 标签名称 -->
        <el-table-column label="标签名称" prop="tagName" width="180"></el-table-column>
        <!-- 创建者 -->
        <el-table-column label="创建者" prop="username" width="180"></el-table-column>
        <!-- 创建日期 -->
        <el-table-column label="创建日期" width="180">
          <template slot-scope="scope">{{ scope.row.addDate|parseTimeByString }}</template>
        </el-table-column>
        <!-- 状态 -->
        <el-table-column label="状态" width="180">
          <template slot-scope="scope">{{ scope.row.state === 1 ? '已启用' : '已禁用' }}</template>
        </el-table-column>
        <!-- 操作 -->
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="text" @click="toggle(scope.row)">{{scope.row.state===1?'禁用':'启用'}}</el-button>
            <el-button type="text" :disabled="scope.row.state===1" @click="showDialogue(scope.row)">修改</el-button>
            <el-button type="text" :disabled="scope.row.state===1||scope.row.totals > 0" @click="del(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="block">
        <el-pagination
          style="margin-top:20px;text-align:right"
          background
          :page-sizes="[5,10,20,50]"
          :page-size="requestParams.pagesize"
          :current-page="requestParams.page"
          @current-change="pager"
          hide-on-single-page
          @size-change="handleSizeChange"
          layout=" prev, pager, next, sizes, jumper"
          :total="total">
        </el-pagination>
      </div>
    </el-card>
    <!-- 新增目录 -->
    <TagsAdd :data="currTag" ref="tagsAdd" @updateList="updateList()"></TagsAdd>
  </div>
</template>

<script>
import { list, remove } from '@/api/hmmm/tags.js'
import TagsAdd from '@/module-hmmm/components/tags-add'
export default {
  components: { TagsAdd },
  data () {
    return {
      requestParams: {
        // list请求需要的数据
        tagName: null,
        state: null,
        // 分页的数据
        page: 1,
        pagesize: 10
      },
      total: 0,
      tags: [],
      currTag: {},
      show: false
    }
  },
  // 获取地址中的参数
  computed: {
    subject () {
      return this.$route.query || {}
    }
  },
  created () {
    this.getList()
  },
  methods: {
    // 清除
    clear () {
      this.requestParams.state = null
    },
    // 筛选
    screen () {
      this.requestParams.page = 1
      this.getList()
    },
    // list请求, 获取标签列表
    async getList () {
      this.requestParams.subjectID = this.subject.id || null
      const { data } = await list(this.requestParams)
      this.tags = data.items
      this.total = data.counts
    },
    // 切换禁用和启用
    async toggle (tag) {
      tag.state = tag.state === 1 ? 0 : 1
    },
    // 切换分页
    pager (np) {
      this.requestParams.page = np
      this.getList()
    },
    // 切换条数
    handleSizeChange (size) {
      this.requestParams.page = 1
      this.requestParams.pagesize = size
      this.getList()
    },
    // 新增目录 显示对话框
    showDialogue (tag = {}) {
      this.currTag = tag
      this.$nextTick(() => {
        this.$refs.tagsAdd.open()
      })
    },
    // 删除
    async del (tag) {
      await this.$confirm('此操作将永久删除该标签, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      await remove(tag)
      this.$message.success('删除成功')
      this.getList()
    },
    // 新增|修改 后更新列表
    updateList () {
      if (!this.currTag.id) {
        this.requestParams.page = 1
      }
      this.getList()
    }

  }

}

</script>

<style lang="scss" scoped>

</style>
